#!/bin/sh -x

NEW_PATH="${2}"/

sqzPlist="/Library/Preferences/com.sorensonmedia.Squeeze 6"
#sqzPlist="/Library/Preferences/com.sorensonmedia.Squeeze Beta"
/usr/bin/defaults write "${sqzPlist}" Path "${NEW_PATH}"

if [ -d "${2}" ]; then
	# gearworks writes a temp file in the application folder
	# so we have to make the folder writable to them
	chmod 777 "${2}"
fi


## setup temporary directory for Squeeze to use when opened by the installer
if [ ! -e "/tmp/SqueezeTemp" ]; then
	pushd "/tmp"
		mkdir SqueezeTemp
		chmod 777 SqueezeTemp
	popd
fi

timeInstalled=`date +"%Y%m%d%H%M%S"`
/usr/bin/defaults write "${sqzPlist}" TimeInstalled "${timeInstalled}"

## delete old version of Squeeze if not update.

## Exit postflight since we are in beta. Comment the line below if we are switching back to release
#exit 0

update=`/usr/bin/defaults read "${sqzPlist}" "Update"`
if [ "${update}" == "0" ]; then

	# get the new installation path:
	newPath=`/usr/bin/defaults read "${sqzPlist}" "Path"`

	# convert canonical form:
	newPath=`(cd "${newPath}"; pwd)`
	
	# get the old installation path,
	# make sure the PrevInstallPath contains "squeeze" in it
	# so that we don't remove some system folder unintentionally:
	oldPath=`/usr/bin/defaults read "${sqzPlist}" "PrevInstallPath" | grep -i squeeze`
	
	if [ -n "${oldPath}" -a -e "${oldPath}" ]; then

		# convert canonical form:
		oldPath=`(cd "${oldPath}"; pwd)`

		if [ "${oldPath}" != "${newPath}" ]; then
			# remove old Squeeze installation:
			rm -rf "${oldPath}"
		fi
	fi
fi


exit 0
